package com.li.onjava8.concurrent.pl;

import com.li.onjava8.concurrent.utils.Timer;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
 * @author li
 * @date 2023/10/30 9:58
 */
public class ParallelPrime {
    static final int COUNT = 100_000;
    public static boolean isPrime(long n){
        return LongStream.rangeClosed(2, (long)Math.sqrt(n)).noneMatch(i -> n % i == 0);
    }

    public static void main(String[] args) {
        Timer timer = new Timer();
        List<String> primes = LongStream.iterate(2, i -> i + 1)
                .parallel()
                .filter(ParallelPrime::isPrime)
                .limit(COUNT)
                .mapToObj(Long::toString)
                .collect(Collectors.toList());
        System.out.println(timer.duration());
    }

}
